package com.youdelu.road;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import me.iwf.photopicker.PhotoPicker;

public class AddCateActivity extends AppCompatActivity  implements  View.OnClickListener{

    private Context mContext;
    private TextView close;
    private EditText name ;
    private ImageView image ;
    private TextView cancel,check,title;
    private String icon_path = null;
    private int id = -1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cate);
        mContext = this ;
        close = findViewById(R.id.close);
        name = findViewById(R.id.name);
        image = findViewById(R.id.image);
        cancel = findViewById(R.id.cancel);
        check = findViewById(R.id.check);
        title = findViewById(R.id.title);

        Intent intent = getIntent();
        id = intent.getIntExtra("id",-1);
        if(id!=-1){
            title.setText("编辑类别");
            cancel.setBackgroundResource(R.drawable.dialog_sharp_delete);
            cancel.setText("删除");
        }
        String mName = intent.getStringExtra("name");
        if(mName!=null){
            name.setText(mName);
        }
        String mImage = intent.getStringExtra("image");
        if(mImage!=null){
            icon_path = mImage;
            Glide.with(mContext).load(icon_path).into(image);
        }
        close.setOnClickListener(this);
        cancel.setOnClickListener(this);
        check.setOnClickListener(this);
        image.setOnClickListener(this);
    }

    /**
     * 打开图片
     */
    public void openSelect(){
        PhotoPicker.builder()
                .setPhotoCount(1)
                .setShowCamera(false)
                .setShowGif(true)
                .setPreviewEnabled(false)
                .start(this, PhotoPicker.REQUEST_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                String path = photos.get(0);
                icon_path = Util.copyFile(mContext,path);
                InputStream open = null;
                try {
                    open = new FileInputStream(new File(icon_path));
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    image.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.close:finish();break;
            case R.id.cancel:
                if(id!=-1){
                    new AlertDialog.Builder(mContext)
                            .setMessage("确认要删除该类别吗？")
                            .setNegativeButton("取消",null)
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    BroadcastManager.getInstance(mContext).sendBroadcast(FileManagerActivity.ACTION_SAVE_CATE,id,"-1#",null);
                                    finish();
                                }
                            }).show();
                }else{
                    finish();
                }
               break;
            case R.id.image:
                openSelect();
                break;
            case R.id.check:
                String mName = name.getText().toString();
                if(mName!=null&&!"".equals(mName)){
                    BroadcastManager.getInstance(mContext).sendBroadcast(FileManagerActivity.ACTION_SAVE_CATE,id,mName,icon_path);
                    finish();
                }else{
                    Toast.makeText(mContext,"请输入名称",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
